test: deflake integration tests by polling instead of fixed sleeps#844
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #844 +/- ##
==========================================
+ Coverage 94.68% 94.82% +0.13%
==========================================
Files 48 48
Lines 5045 5045
==========================================
+ Hits 4777 4784 +7
+ Misses 268 261 -7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
44e03e0 to
ad43eec
Compare
Add a wall-clock-deadline `poll_until_condition` helper, generalize `maybe_await` to any awaitable, and refactor `collect_iterate_until_present` to reuse a shared drain step.
Lock writes propagate asynchronously after `list_and_lock_head` returns, so unlocking immediately could see fewer locks than acquired; poll `list_head` until the locked IDs disappear from the queue head before unlocking.
1cb554e to
e23e54d
Compare
janbuchar
approved these changes
Jun 3, 2026
vdusek
added a commit
to apify/crawlee-python
that referenced
this pull request
Jun 4, 2026
…#1926) Unifies eventually-consistent / settling waits across the test suite behind a single `poll_until_condition` helper, matching the form shared with apify/apify-client-python#844. ## Changes - **Single helper.** Replace `wait_for_condition` with `poll_until_condition` (`tests/_utils.py`): it polls a sync-or-async callable until an optional `condition` holds (default: truthy) or a wall-clock `timeout` expires, then returns the last result so the caller asserts explicitly. Exponential backoff is folded into a `backoff_factor` argument instead of a separate `call_with_exp_backoff`. - **Shared location + relative imports.** Move the helper from `tests/unit/utils.py` to the shared `tests/_utils.py` and import it relatively. This requires the test tree to be a proper package, so add `__init__.py` to the `tests/unit` and `tests/e2e` subpackages that lacked one (and allow `TID252` in tests). - **Migrate hand-rolled waits.** Replace fixed `sleep`s and `for`/`while` polling loops with the helper: - 5 autoscaling waits — `test_autoscaled_pool.py` - statistics-initialization loop — `test_basic_crawler.py` - 2 sitemap-loader waits — `test_sitemap_request_loader.py` - 2 request-queue background-task waits — `test_request_queue.py` Lint, type-check, and the full unit suite (1883 passed) pass.
vdusek
added a commit
to apify/apify-sdk-python
that referenced
this pull request
Jun 5, 2026
…flake via polling (#920) ## Summary The SDK counterpart of apify/apify-client-python#844: introduce a single shared `tests/_utils.py` with one polling helper, and migrate the whole test suite to it — replacing fixed sleeps and hand-rolled retry loops that caused flakiness. ## Changes - **Shared `tests/_utils.py`.** One `poll_until_condition(fn, condition=bool, *, timeout, poll_interval, backoff_factor)` helper — identical to the one in apify-client-python#844 — polls a sync-or-async callable until a condition holds or a wall-clock timeout expires. `backoff_factor=2` subsumes the former `call_with_exp_backoff`; `timeout=0` is the "call once" case. The `maybe_await` adapter, `generate_unique_resource_name`, and the shared RSA crypto test keys move here too. The per-package `tests/integration/_utils.py` and `tests/e2e/_utils.py` are removed, and cross-test imports (e.g. the crypto keys, previously imported from the `test_crypto` module) now go through this single module. - **Request queue tests.** ~50 polling call sites in `test_request_queue.py` migrated to `poll_until_condition`. The single/shared timeout is centralized in an `rq_poll_timeout` fixture (`timeout=0` single, `timeout=30` shared), backed by an `rq_access_mode` fixture — replacing the access-mode derivation block previously copy-pasted into every test. The shared-mode request-ordering relaxations from #931 are preserved on top. - **Deflaked tests.** Fixed the flaky `test_actor_adds_webhook_and_receives_event` e2e test (the client now stays alive 5 s after `add_webhook`, and the unbounded `INITIALIZED` startup loop becomes bounded polling) — replaces #930. Replaced the `retry_counter` loops in `test_actor_charge.py` (4×) and fixed sleeps in `test_actor_lifecycle`, `test_actor_key_value_store`, and `test_apify_event_manager` with condition polling. Fixed sleeps that are semantically required are intentionally kept: negative checks ("event must NOT fire"), mtime-granularity checks, simulated latency, and sleeps inside deployed Actor `main` bodies (where the helper is unavailable). Verified: lint, type-check, and the full unit suite pass; integration/e2e require a platform token and run in CI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Started as a fix for this flaky CI run of
test_request_queue_unlock_requests[sync](assert 2 == 3onunlocked_count, caused by replication lag betweenlist_and_lock_headandunlock_requests), and grew into deflaking the integration test suite as a whole.Changes:
poll_until_conditionhelper totests/integration/_utils.py: polls a sync-or-async callable at a constant interval until a condition holds or a wall-clock timeout expires. An optionalbackoff_factormultiplies the interval after each poll, for highly variable waits (e.g. Actor run container startup) where a growing delay covers a long timeout with few calls.list_headuntil the locked IDs disappear from the queue head before unlocking.for _ in range(5): sleep(1); read; breakpolling loops (10×) and single fixedsleep(1)waits (26×) across the request queue, dataset, key-value store, and run integration tests withpoll_until_condition. This makes the tests both faster on the happy path (no unconditional sleep) and more robust under load (polls until the timeout instead of hoping 1 s is enough).maybe_awaitto accept any awaitable.The three
iterate_keyssleeps in the KVS tests are intentionally left as-is: draining an iterator per attempt wants attempt-count semantics (likecollect_iterate_until_present), not a wall-clock deadline.Follow-up to #786.